<template>
  <div class="article-create">
    <h1>发表新闻</h1>
    <form @submit.prevent="submitArticle">
      <div class="form-group">
        <label>标题</label>
        <input v-model="form.title" type="text" required />
      </div>
      <div class="form-group">
        <label>摘要</label>
        <textarea v-model="form.summary" required></textarea>
      </div>
      <div class="form-group">
        <label>内容</label>
        <textarea v-model="form.content" rows="10" required></textarea>
      </div>
      <button type="submit">发表</button>
    </form>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { add, TableName } from '@/api/index.js';
import { useAuthStore } from '@/stores/auth.js';

export default {
  setup() {
    const router = useRouter();
    const authStore = useAuthStore();

    const form = ref({
      title: '',
      summary: '',
      content: '',
      userid: authStore.user.id,
      username: authStore.user.username,
      typeIndex: TableName.Articles
    });

    const submitArticle = async () => {
      const result = await add(form.value);
      if (result && result.errno === 0) {
        router.push(`/articles/${result.data.id}`);
      }
    };

    return {
      form,
      submitArticle
    };
  }
};
</script>

<style scoped>
.article-create {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}
.form-group {
  margin-bottom: 15px;
}
.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}
.form-group input,
.form-group textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
button {
  padding: 10px 20px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>